from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# 打开浏览器
driver = webdriver.Firefox()
time.sleep(2)
# 设置窗口大小
driver.set_window_size(400, 800)
time.sleep(2)
# 双击打开百度页面
driver.get('https://www.baidu.com/')
time.sleep(2)
# 窗口最大化
driver.maximize_window()
time.sleep(2)
time.sleep(2)
seatch = driver.find_element(By.ID, 'kw')
# 键盘输入事件
seatch.send_keys('selenium键盘鼠标浏览器事件')
time.sleep(2)
# 键盘回车事件
seatch.send_keys(Keys.ENTER)
time.sleep(2)
# 浏览器：返回
driver.back()
time.sleep(2)
# 浏览器：前进
driver.forward()
time.sleep(2)
# 输出浏览器信息
print(f'浏览器名称：{driver.name}，浏览器尺寸：{driver.get_window_size()}, 浏览器当前URL：{driver.current_url}，浏览器标题：{driver.title}')
# 元素坐标
print(seatch.location)
# 元素尺寸
print(seatch.size)
# 元素是否可用
print(seatch.is_enabled())
# 元素是否显示
print(seatch.is_displayed())
# 元素是否选中（单选框，复选框）
print(seatch.is_selected())

# 鼠标右击
ActionChains(driver).context_click(seatch).perform()
time.sleep(2)
# 鼠标三击
ActionChains(driver).double_click(seatch).click(seatch).perform()
time.sleep(2)
# 键盘全选
# seatch.send_keys(Keys.CONTROL, 'a')
# time.sleep(2)
# 键盘复制事件
seatch.send_keys(Keys.CONTROL, 'c')
time.sleep(2)
# 点击事件
seatch.click()
time.sleep(2)
# 键盘悬停事件
set = driver.find_element(By.NAME, 'tj_settingicon')
ActionChains(driver).move_to_element(set).perform()
time.sleep(2)
# 点击高级搜索
driver.find_element(By.LINK_TEXT, '高级搜索').click()
time.sleep(2)
q3 = driver.find_element(By.NAME, 'q3')
# 键盘粘贴事件
q3.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
# 鼠标双击事件
ActionChains(driver).double_click(q3).perform()
time.sleep(2)
# 键盘剪切事件
q3.send_keys(Keys.CONTROL, 'x')
time.sleep(2)
# 键盘粘贴事件
q2 = driver.find_element(By.NAME, 'q2')
q2.send_keys(Keys.CONTROL, 'v')
time.sleep(2)
# 键盘点击事件
# 点击时间限制
driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[1]/i[1]').click()
time.sleep(2)
# 点击一月内
driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[2]/div[2]/p[4]').click()
time.sleep(2)
# 点击所有网页和文件
driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[1]/i[1]').click()
time.sleep(2)
# 点击PDF
driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[2]/div[2]/p[2]').click()
time.sleep(2)
# 单选框网页位置
driver.find_element(By.XPATH, '//*[@id="q5_1"]').click()
time.sleep(2)
# 输入站内搜索网址
driver.find_element(By.NAME, 'q6').send_keys('baidu.com')
time.sleep(2)
# 点击高级搜索按钮
driver.find_element(By.CSS_SELECTOR, '#adv-setting-8 > input.advanced-search-btn.c-btn.c-btn-primary.switch-button').click()

time.sleep(10)
# 关闭浏览器
driver.quit()



